-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.py
85 lines (62 loc) · 2.12 KB
/
contract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from ontology.interop.System.ExecutionEngine import GetCallingScriptHash, GetEntryScriptHash
from ontology.interop.System.Runtime import CheckWitness
from ontology.interop.System.Storage import GetContext, Get, Put, Delete
from ontology.interop.Ontology.Runtime import Base58ToAddress
ctx = GetContext()
APPROVAL_PREFIX = 'APPROVAL-PREFIX-KEY'
OWNER = Base58ToAddress('<BASE-58-ADDRESS')
def Main(operation, args):
if operation == 'notProtectedFromCCA':
Require(len(args) == 0)
return notProtectedFromCCA()
elif operation == 'protectedFromCCA':
Require(len(args) == 0)
return protectedFromCCA()
elif operation == 'approveContract':
Require(len(args) == 1)
contractHash = args[0]
return approveContract(contractHash)
elif operation == 'unapproveContract':
Require(len(args) == 1)
contractHash = args[0]
return unapproveContract(contractHash)
elif operation == 'isApproved':
Require(len(args) == 1)
contractHash = args[0]
return isApproved(contractHash)
return False
def notProtectedFromCCA():
return True
def protectedFromCCA():
RequireApproved()
return True
def approveContract(contractHash):
RequireOwner()
RequireNotContract()
key = getApprovalKey(contractHash)
Put(ctx, key, True)
def unapproveContract(contractHash):
RequireOwner()
RequireNotContract()
key = getApprovalKey(contractHash)
Delete(ctx, key)
def RequireApproved():
callerHash = GetCallingScriptHash()
entryHash = GetEntryScriptHash()
if entryHash is not callerHash:
approved = isApproved(callerHash)
Require(approved)
def isApproved(contractHash):
key = getApprovalKey(contractHash)
return Get(ctx, key)
def getApprovalKey(contractHash):
return concat(APPROVAL_PREFIX, contractHash)
def RequireOwner():
Require(CheckWitness(OWNER))
def RequireNotContract():
callerHash = GetCallingScriptHash()
entryHash = GetEntryScriptHash()
Require(callerHash == entryHash)
def Require(expr):
if not expr:
raise Exception('Expression resulted in false')